package com.xszx.server.service;

import com.xszx.server.dto.Page;
import com.xszx.server.entity.Role;
import com.xszx.server.mapper.RoleMapper;
import com.xszx.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;

@Service
public class RoleService {
    @Resource
    private RoleMapper roleMapper;

    /**
     * 获取分页列表
     * @param page
     * @return
     */
    public Page getAllRoles(Page page){
        int pageNum = page.getPage();
        page.setPage((page.getPage()-1)*page.getSize());
        page.setList(roleMapper.findAll(page));
        page.setTotal(roleMapper.findCount());
        page.setPage(pageNum);
        return page;
    }

    /**
     * 新增或者删除信息
     * @param role
     * @return
     */
    public String saveOrUpdate(Role role){
        if(StringUtils.isEmpty(role.getId())){
            role.setId(UuidUtil.getShortUuid());
            return (roleMapper.insert(role)>0)?"添加成功":"添加失败";
        }else{
            return (roleMapper.updateByPrimaryKey(role)>0)?"更新成功":"更新失败";
        }
    }

    /**
     * 物理删除角色
     * @param id
     * @return
     */
    public int del(String id){
        return roleMapper.deleteByPrimaryKey(id);
    }
     /**
     * 获取单个角色
     * @return
     */
    public Role getRole(String id){
        return roleMapper.selectByPrimaryKey(id);
    }
}
